package de.koller.gravity.scene.kdLogo;

import de.koller.aframe.plain.PlainRasterFloat;
import de.koller.aframe.util.misc.ColorOperations;
import de.koller.aframe.util.noise.n2D.Noise2D;
import de.koller.aframe.util.noise.n2D.Perlin2D;
import de.koller.aframe.util.resource.IOLoader;
import de.koller.oceanFramework.gl.util.ImagePreview;
import de.koller.oceanFramework.gl.util.RasterManager;
import de.koller.oceanFramework.math.RVec3;
import java.awt.image.BufferedImage;
import java.util.Random;

public class KDLogoSzene {
	private final Random rnd = new Random();
	
	private final RasterManager plain;
	
	private final PlainRasterFloat raster = new PlainRasterFloat( 1024, 1024 );
	private final Noise2D noise;
	private final Perlin2D perlin;
	
	public KDLogoSzene() {
		Perlin2D.PRINT_PROGRESS = true;
		perlin = new Perlin2D( 0, 6, 0.8f, 2, rnd );
		perlin.normalizeMaxBound( Perlin2D.maxHeight( 0, 6, .8f ) );
		
		noise = new Noise2D( new float[ raster.width ][ raster.height ] );
		
		plain = IOLoader.create( IOLoader.loadLocalImage( "./resources/logo/kdlogo.png" ), false );
		//Convolution.lin( plain, plain, Convolution.cubicFilter( 10 ), 10 );
		
		ImagePreview.showRaster( plain, 512, 512 );
	}
	
	private int getPlainColor( int x, int y ) {
		int ix = x;
		int iy = y;
		
		if( ix < 0 ) return 0x00000000;
		if( iy < 0 ) return 0x00000000;
		if( ix >= plain.getWidth() ) return 0x00000000;
		if( iy >= plain.getHeight() ) return 0x00000000;
		
		return plain.colorAt( ix, iy );
	}
	
	private void renderImage( BufferedImage img ) {
//		for( int x = 0; x < raster.width; x++ )
//			for( int y = 0; y < raster.height; y++ ) {
//				float ox = 1f / raster.width  * x * plain.getWidth();
//				float oy = 1f / raster.height * y * plain.getHeight();
//				
//				int c = getPlainColor( (int)ox, (int)oy );
//				raster.setValue( 1f - ColorIntUtil.getRValueF( c ), x, y );
//			}
//		
//		for( int x = 0; x < raster.width; x++ )
//			for( int y = 0; y < raster.height; y++ ) 
//				noise.noise[x][y] = raster.getValue( x, y );
//		Function2DPreview.showFunction( 0, 0, 1, 1, noise );
//		
//		for( int x = 0; x < perlin.noise.length; x++ )
//			for( int y = 0; y < perlin.noise.length; y++ ) 
//				perlin.noise[x][y] *= .5f * noise.getValue( 1f / perlin.noise.length * x, 1f / perlin.noise.length * y );
//		Function2DPreview.showFunction( 0, 0, 1, 1, perlin );
		
	
		for( int x = 0; x < img.getWidth(); x++ )
			for( int y = 0; y < img.getHeight(); y++ ) {
				RVec3 n = perlin.getDerivate( 1f/img.getWidth() * x, 1f/img.getHeight() * y );
				RVec3 lightVector = new RVec3( -.5f, -.5f, 1 ).normalize();
				RVec3 eyevector = new RVec3( 0, 0, 1 );
				
				int a = 0xff101010;
				
				int d = ColorOperations.diffuseLight( 
						lightVector,
						n,
						0xff303030,
						0xff505050 );

				int s = ColorOperations.specularLight(
						lightVector,
						n,
						eyevector,
						0xffc0c0c0,
						0xffc0c0c0,
						10 );

				img.setRGB( x, y, ColorOperations.lightColor( 0xffffffff, a, d, s ) );
			}
	}
	
	public BufferedImage getLogo() {
		BufferedImage i = new BufferedImage( 512, 512, BufferedImage.TYPE_INT_ARGB );
		renderImage( i );
		return i;
	}
	
	public static void main( String[] args ) {
		KDLogoSzene szene = new KDLogoSzene();
		ImagePreview.showImage( szene.getLogo(), 512, 512 );
	}
	
}
